package com.scnu.hachimi.show;

import com.scnu.hachimi.manager.ResourcesManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Map;

public class indexJPanel extends JPanel {
    private int state = 1;
    private Map<Integer, String> map = new java.util.HashMap<>();
    private ResourcesManager rm;
    private MainJFrame mjf;

    public indexJPanel(ResourcesManager rm, MainJFrame mjf) {
        this.rm = rm;

        this.setPreferredSize(new Dimension(800, 600));
        this.setFocusable(true);
        this.mjf = mjf;

        map.put(1, "single");
        map.put(2, "coupe");
        map.put(3, "map");

        this.addKeyListener(new KeyboardListener());
    }

    public void paint(Graphics g) {
        Image img = rm.imageMap.get(map.get(state));
        g.drawImage(img, 0, 0, null);
        g.drawString("当前地图：", 0, 0);
    }



    //键盘监听器，监听w，s，回车
    public class KeyboardListener extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_W:
                    state = state > 1 ? state - 1 : state;
                    repaint();
                    break;
                case KeyEvent.VK_S:
                    state = state < 3 ? state + 1 : state;
                    repaint();
                    break;
                case KeyEvent.VK_ENTER:
                    if (state != 3) {
                        try {
                            mjf.showOption(state);
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                    else
                        mjf.showEditor();
                    break;

            }
        }
    }
}
